/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.view.labels;

import org.testis.gui.mvc.utilities.TristateCheckBox;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreePath;

import org.testis.utils.tree.TristateLabelTreeNode;

/**
 *
 * @author selcuk
 */
public class TristateLabelTreeNodeEditor extends AbstractCellEditor implements TreeCellEditor {

    TristateLabelTreeNodeRenderer renderer = new TristateLabelTreeNodeRenderer();
    ChangeEvent _changeEvent = null;
    JTree tree;

    public TristateLabelTreeNodeEditor(JTree tree) {
        this.tree = tree;
    }

    @Override
    public Object getCellEditorValue() {
        TristateCheckBox checkbox = renderer.getRenderer();
        TristateLabelTreeNode labelTreeNode = renderer.getLabelTreeNode();
        labelTreeNode.setState(checkbox.getState());
        return labelTreeNode;
    }

    @Override
    public boolean isCellEditable(EventObject event) {
        boolean returnValue = false;
        if (event instanceof MouseEvent) {
            MouseEvent mouseEvent = (MouseEvent) event;
            TreePath path = tree.getPathForLocation(mouseEvent.getX(),
                    mouseEvent.getY());
            if (path != null) {
                Object node = path.getLastPathComponent();
                if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
                    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                    Object userObject = treeNode.getUserObject();
                    returnValue = ((!treeNode.isRoot()) && (userObject instanceof TristateLabelTreeNode));
                }
            }
        }
        return returnValue;
    }

    @Override
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
        Component editor = renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf, row, true);
        // editor always selected / focused
        ItemListener itemListener = new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent itemEvent) {
                if (stopCellEditing()) {
                    fireEditingStopped();
                }
            }
        };
        if (editor instanceof TristateCheckBox) {
            ((TristateCheckBox) editor).addItemListener(itemListener);
        }
        return editor;
    }

    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Tristate Tree");
                f.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });

                TristateLabelTreeModelLoader modelLoader = new TristateLabelTreeModelLoader();
                DefaultTreeModel model = modelLoader.loadTreeModel();

                JTree labelTree = new JTree(model);
                labelTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//                labelTree.putClientProperty("JTree.lineStyle", "Angled");
                labelTree.setCellRenderer(new TristateLabelTreeNodeRenderer());
                labelTree.setCellEditor(new TristateLabelTreeNodeEditor(labelTree));
                labelTree.setEditable(true);
                labelTree.setExpandsSelectedPaths(false);

                f.getContentPane().add(labelTree);
                f.pack();
                f.setSize(300, 300);
                f.setVisible(true);
            }
        });
    }
     */
}
